package com.superteam.mac.web.action;

import java.sql.Blob;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialBlob;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.superteam.mac.base.web.action.BaseAction;
import com.superteam.mac.mode.MachineRec;
import com.superteam.mac.mode.WorkShop;
import com.superteam.mac.web.form.WorkShopForm;

public class WorkshopAction extends BaseAction {
	/**
	 * 获得所有的车间 和 机床
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward findWorkShopAndMachine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			List<WorkShop> workshops = super.getWrokShopService().findWorkShopAndMachines();
			for(WorkShop work:workshops)
			{
				List<MachineRec> machines = work.getMachines();
				for(MachineRec machine:machines)
				{
					System.out.println(work.getWorkShopName()+"++88888++++++"+machine.getMachineName());
				}
			}
			request.setAttribute("shops", workshops);
		return mapping.findForward("showworkshop");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward findAllWorkShop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			List list = super.getWrokShopService().findAllWrokShop(0, 100);
			request.setAttribute("workshops", list);
			
		return mapping.findForward("success");
	}
	
	public ActionForward findWorkShop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return null;
	}
	
	public ActionForward addWorkShop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WorkShopForm workform = (WorkShopForm)form;
		WorkShop work = new WorkShop();
		work.setWorkShopCode(workform.getWorkShopCode());
		work.setWorkShopName(workform.getWorkShopName());
		work.setLogName(workform.getLogName());
		work.setLogCode(workform.getLogCode());
		work.setIpAddress(workform.getIpAddress());
		
		byte [] data = workform.getImg().getFileData();		
		System.out.println(data.length);
		Blob blob = new SerialBlob(data);
		System.out.println(blob.length());
		work.setLayoutMap(blob);
		super.getWrokShopService().saveWorkShop(work);
		return mapping.findForward("addsuccess");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward delWorkShop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//request.setCharacterEncoding("GBK");
		String code = request.getParameter("delwork");
		super.getWrokShopService().deleteWorkShop(code);
		List list = super.getWrokShopService().findAllWrokShop(0, 100);
		request.setAttribute("workshops", list);
		return mapping.findForward("success");
	}
}
